
package com.recruit.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.recruit.dao.gen.mapper.UserActionMapper;
import com.recruit.dao.gen.model.UserAction;
import com.recruit.dao.gen.model.UserActionExample;
import com.recruit.dao.gen.model.UserActionExample.Criteria;
import com.recruit.dao.impl.JobDao;
import com.recruit.dto.JobDto;
import com.recruit.service.UserActionService;

@Service("userActionService")
public class UserActionServiceImpl implements UserActionService{
	@Resource
	private UserActionMapper userActionMapper;

	@Resource
	private JobDao jobDao;
	
	@Override
	public List<UserAction> queryByUser(int userId) {
		// TODO Auto-generated method stub
		UserActionExample userActionExample = new UserActionExample();
		userActionExample.setOrderByClause("click_count desc");
		Criteria createCriteria = userActionExample.createCriteria();
		createCriteria.andUserIdEqualTo(userId);
		List<UserAction> list = userActionMapper.selectByExample(userActionExample);
		return list;
	}
	
	@Override
	public List<UserAction> queryByUserAndJobType(int userId, String jdbcStatus) {
		// TODO Auto-generated method stub
		UserActionExample userActionExample = new UserActionExample();
		userActionExample.setOrderByClause("click_count desc");
		Criteria createCriteria = userActionExample.createCriteria();
		
		createCriteria.andUserIdEqualTo(userId);
		createCriteria.andJobTypeEqualTo(jdbcStatus);
		List<UserAction> list = userActionMapper.selectByExample(userActionExample);
		return list;
	}

	@Override
	public int insert(UserAction userAction) {
		// TODO Auto-generated method stub
		return userActionMapper.insertSelective(userAction);
	}

	@Override
	public int update(UserAction userAction) {
		// TODO Auto-generated method stub
		return userActionMapper.updateByPrimaryKeySelective(userAction);
	}
	
	@Override
	public List<JobDto> recommend(Integer userId) {
		// TODO Auto-generated method stub
		
		List<UserAction> userActions = queryByUser(userId);
		
		List<JobDto> jobDtos = new ArrayList<>();
		for (UserAction userAction : userActions) {
			List<JobDto> findByJobStatus = jobDao.findByJobStatus(userAction.getJobType());
			if (jobDtos.size() < 3) {
				if (null != findByJobStatus && !findByJobStatus.isEmpty()) {
					jobDtos.addAll(findByJobStatus);
				}
			}else {
				break;
			}
		}
		
		return jobDtos;
	}
	
	
}
